package hotspot;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import Vis.User;
import bodyPosition.Position;
import bodyPosition.PositionStorage;

public class SpotMap extends JPanel{

	protected int width, height;
	protected int MAX_X = 650;
	protected int MAX_Z = 2300;
	private HotspotApp hotspotApp;
	protected int range = 80;
	public JPanel container;
	
	public SpotMap(int w, int h, HotspotApp ha){
		width = w;
		height = h-45;
		
		hotspotApp = ha;

		this.setPreferredSize(new Dimension(width, height));
		
		container = new JPanel(new BorderLayout());		
		/*container.add(new JLabel("Hotspots"), BorderLayout.NORTH);*/

		TitledBorder t = BorderFactory.createTitledBorder("HOTSPOTS");
		t.setTitleJustification(TitledBorder.CENTER);
		container.setBorder(t);
		
		container.add(this, BorderLayout.CENTER);
		JButton clearButton = new JButton("Clear all");
		clearButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				hotspotApp.clearHotspots();				
			}
		});		
		container.add(clearButton, BorderLayout.NORTH);		
	}
	
	public SpotMap(HotspotApp ha){
		this(540,320, ha);
	}
	
	@Override
	protected void paintComponent(Graphics g){
		
		//background
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		g.setColor(Color.BLACK);
		g.drawRect(5, 5, width-5, height-5);
		
		//g.setFont(new Font("Verdana", Font.PLAIN, 20));
		//axis
		//g.setColor(Color.BLACK);
		//g.drawLine(20, height-20, width-20, height-20);
		//g.drawLine(20, 20, 20, height-20);
		//System.out.println(getXRange() + " " + getZRange());
		//hotspots
		for(Hotspot h:hotspotApp.hotspots){

			g.setColor(Color.LIGHT_GRAY);
			g.drawRect(fitWidth(h.getX()), fitHeight(h.getZ()), 
					getXRange(), getZRange());
			g.setColor(Color.BLACK);
			g.fillOval(fitWidth(h.getX()), fitHeight(h.getZ()), 
					getXRange(), getZRange());
					   //20,20);
			g.drawString(h.getText(), fitWidth(h.getX())-(getXRange()/2), 
									  fitHeight(h.getZ()));
		}
		//onHotspot
		if(hotspotApp.onHotspot!=null){
			g.setColor(Color.LIGHT_GRAY);
			g.drawRect(fitWidth(hotspotApp.onHotspot.getX()), //x 
					   fitHeight(hotspotApp.onHotspot.getZ()), //z
					   getXRange(), getZRange());
			g.setColor(Color.RED);
			g.fillOval(fitWidth(hotspotApp.onHotspot.getX()), //x 
					   fitHeight(hotspotApp.onHotspot.getZ()), //z
					   getXRange(), getZRange());
					   //20,20);
			g.drawString(hotspotApp.onHotspot.getText(), 
					fitWidth(hotspotApp.onHotspot.getX())-(getXRange()/2), 
					fitHeight(hotspotApp.onHotspot.getZ()));
		}
		
		//draw all users
		Collection<User> c =hotspotApp.users.getUsers();
		
		for(User user:c){
			PositionStorage ps = user.getPositionStorage();
			for(Position p: ps.getAllPositions()){
				double[] currentPos;
				synchronized(p){
					currentPos = p.getPosition();
				}
				if(p != null){

					g.setColor(Color.LIGHT_GRAY);
					g.drawRect(fitWidth(currentPos[0]), //x 
							   fitHeight(currentPos[2]), //z
							   getXRange(), getZRange());
					g.setColor(ps.getColor());
					g.fillOval(fitWidth(currentPos[0]), //x 
							   fitHeight(currentPos[2]), //z
							   getXRange(), getZRange());
					   //20,20);
					g.drawString("User "+user.getID(), 
							fitWidth(currentPos[0]), //x 
							   fitHeight(currentPos[2]));
				}
			
			}
		}
		
	}
	
	private int fitWidth(double w){
		w=w/1000;
		return (int)((w/MAX_X) * width); 
	}
	
	private int fitHeight(double h){
		h=h/1000;
		return (int)((h/MAX_Z) * height);
	}
	
	protected int getXRange(){
		return fitWidth(hotspotApp.xRange*1000);
	}
	
	protected int getZRange(){
		return fitHeight(hotspotApp.zRange*1000);
	}
	
	
	
}
